package sk.octopuss.archery.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sk.octopuss.archery.dao.ItemDao;
import sk.octopuss.archery.domain.Category;
import sk.octopuss.archery.domain.Item;
import sk.octopuss.archery.service.AbstractGenericService;
import sk.octopuss.archery.service.ImageService;
import sk.octopuss.archery.service.ItemService;
@Service
public class ItemServiceImpl extends AbstractGenericService implements ItemService{
	
	@Autowired
	ItemDao itemDao;
	
	@Autowired
	ImageService imageService;
	
	@Override
	public Item find(final Long id) {
		return itemDao.find(id);
	}

	@Override
	public List<Item> findAll() {
		return itemDao.findAll();
	}

	public ItemDao getItemDao() {
		return itemDao;
	}

	public void setItemDao(final ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	@Override
	public Item findByCatalogueNumber(final String catalogueNumber) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Item> findAllByName(final String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Category> getParentCategories(final Item item) {
		return itemDao.getParentCategories(item);
	}

	@Override
	public List<Category> getParentCategoriesFromTop(final Item item) {
		final List<Category> categories = itemDao.getParentCategories(item);
		Collections.reverse(categories);
		return categories;
	}

	@Override
	public List<Item> findAllEntries(final int firstResult, final int maxResults) {
		return itemDao.findAllEntries(firstResult, maxResults);
	}

	@Override
	public long countItems() {
		return itemDao.countAll();
	}

	@Override
	@Transactional
	public long save(final Item item) {
		
		return itemDao.save(item);
		
	}

	@Override
	public void remove(final Item entity) {
		
		imageService.removeImage(entity.getThumbnail());
		itemDao.delete(entity);
		
	}
	
}
